//
//  CustomTabBarView.m
//  hylink
//
//  Created by 李迪 on 14-9-6.
//  Copyright (c) 2014年 lidi. All rights reserved.
//

#import "CustomTabBarView.h"
#import "CustomTabBarItem.h"
#import "Masonry.h"
#import "VerticelAlignButton.h"

#import "UIControl+Blocks.h"

@interface CustomTabBarView()
@property(nonatomic,strong)NSMutableArray *items;
@property(nonatomic,strong)CALayer *topLineLayer;
@end

@implementation CustomTabBarView

+ (instancetype)customTabBarViewWithSelectAtIndexBlock:(void (^)(NSUInteger))selectAtIndexBlock_
{
    CustomTabBarView *instance = [CustomTabBarView new];
    instance.selectAtIndexBlock = selectAtIndexBlock_;
    instance.items = [NSMutableArray array];
    
    CGFloat itemWidth = [UIScreen mainScreen].bounds.size.width / 5;
    
    //底图
    UIView *maskView = [UIView new];
    maskView.backgroundColor = [UIColor whiteColor];
    [instance addSubview:maskView];
    [maskView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(itemWidth, 49.0f));
        make.bottom.equalTo(maskView.superview);
        make.centerX.equalTo(maskView.superview);
    }];
    
//    //网格员
//    CustomTabBarItem *buttonGridService = [CustomTabBarItem customTabBarItemWith:@"tab_shortcut"
//                                                  selectedImageName:@"tab_shortcut"
//                                                              title:@"我的"];
//    UITapGestureRecognizer *gridServiceTap = [[UITapGestureRecognizer alloc] initWithTarget:instance action:@selector(gridService:)];
//    [buttonGridService addGestureRecognizer:gridServiceTap];
//    [instance addSubview:buttonGridService];
//
//    //约束
//    [buttonGridService mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.size.mas_equalTo(CGSizeMake(itemWidth, 49.0f));
//        make.bottom.equalTo(buttonGridService.superview);
//        make.right.equalTo(buttonGridService.superview);
//    }];

    NSArray *tabDatas = @[@{@"normalImageName": @"tab_home_normal",@"selectedImageName":@"tab_home_selected"},
                          @{@"normalImageName": @"tab_talk_normal",@"selectedImageName":@"tab_talk_selected"},
                          
                          @{@"normalImageName": @"",@"selectedImageName":@""},
                          
                          @{@"normalImageName": @"tab_my_normal",@"selectedImageName":@"tab_my_selected"},
                          @{@"normalImageName": @"tab_shortcut",@"selectedImageName":@"bot_member_c"}];
    NSArray *titles = @[@"新闻",@"楚天+",@"",@"掌报",@"我的"];
    
    CGFloat x_ = 0;
    
    for (int i=0;i<tabDatas.count;i++) {
        
        NSDictionary *tabData = [tabDatas objectAtIndex:i];
        
        CustomTabBarItem *item = [CustomTabBarItem customTabBarItemWith:[tabData objectForKey:@"normalImageName"]
                                                      selectedImageName:[tabData objectForKey:@"selectedImageName"] title:titles[i]];
        item.tag = i;
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:instance action:@selector(didSelectTab:)];
        
        [item addGestureRecognizer:tap];
        
        [instance addSubview:item];
        
        [instance.items addObject:item];
        
        //约束
        [item mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.equalTo(item.superview);
            make.left.equalTo(item.superview).offset(x_);
            make.width.equalTo(@(itemWidth));
        }];
        
        x_ += itemWidth;
        
        if (!i) {
            item.isSelected = YES;
        }
    }
    
    
    UILabel *line = [UILabel new];
    line.backgroundColor = [UIColor colorWithRed:206/255.0f green:206/255.0f blue:206/255.0f alpha:1.0f];
    [instance addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(line.superview);
        make.left.equalTo(line.superview);
        make.right.equalTo(line.superview);
        make.height.equalTo(@(.5f));
    }];
    
    // 加图片在中间
    UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_tab_center"]];
    imgView.userInteractionEnabled = NO;
    [instance addSubview:imgView];
    [imgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(45.0f);
        make.height.mas_equalTo(41.0f);
        make.centerX.equalTo(imgView.superview);
        make.bottom.equalTo(imgView.superview);
    }];
    
    return instance;
}

- (void)awakeFromNib
{
    [super awakeFromNib];
    self.layer.masksToBounds = NO;
}

- (void)didSelectTab:(UIGestureRecognizer *)ges
{
    CustomTabBarItem *item = (CustomTabBarItem *)ges.view;
    for (CustomTabBarItem *item_ in self.items) {
        if (item == item_) {
            item_.isSelected = YES;
        }
        else
        {
            item_.isSelected = NO;
        }
    }
    
    if (self.selectAtIndexBlock) {
        self.selectAtIndexBlock(item.tag);
    }
    
}

- (void)selectItemAtIndex:(NSInteger)index
{
    for (int i=0; i<self.items.count; i++) {
        CustomTabBarItem *item = [self.items objectAtIndex:i];
        if (i==index) {
            item.isSelected = YES;
        }
        else
        {
            item.isSelected = NO;
        }
    }
    
    if (self.selectAtIndexBlock) {
        self.selectAtIndexBlock(index);
    }
}

- (void)gridService:(id)sender
{
    if (self.gridServiceBlock) {
        self.gridServiceBlock();
    }
}

@end
